package com.scuvc.dao.impl;

import com.scuvc.dao.UserInfoDao;
import com.scuvc.entity.UserInfo;
import com.scuvc.utils.ConnectionFactory;
import com.scuvc.utils.StaticUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserInfoDaoImpl implements UserInfoDao {
    private QueryRunner queryRunner = new QueryRunner
            (ConnectionFactory.getDruidDataSource());
    @Override
    public List<UserInfo> getUserByAccount(String account) {
        String sql = "SELECT * FROM eh_userinfo WHERE user_name=?";
        List<UserInfo> list = null ;
        try {
            list = queryRunner.query(sql,new BeanListHandler
                    <UserInfo>(UserInfo.class),account);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int insert(UserInfo userInfo) {
        String sql = "INSERT INTO eh_userInfo VALUES(NULL,?,?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql,userInfo.getUser_name(),userInfo.getPassword(),
                    userInfo.getGender(),userInfo.getBirthday(),userInfo.getCardNo(),
                    userInfo.getDriverID(),userInfo.getMobile());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static void main(String[] args) {
        List<UserInfo> list = StaticUtils.userInfoDao.getUserByAccount("sum");
        System.out.println(list);
    }
}
